const app = getApp()
const $api = require('../../utils/api.js').API;
Page({
  data: {
      job:[],
      index:0,
      city:'天津',
      region: ['天津市', '天津市', ''],
      workTime: ['1年', '2年', '3年','4年','5年以上'],
      groupNum: ['50', '100'],
      sendNum:0,
      exp:'',
      num:50,
      message:'',
      groupId:0
  },
  onLoad: function(e){
    this.setData({
      num:e.num,
      groupId:e.groupId
    })
    this.getJob()
  },
  getJob:function(){
    var that = this
    var data ={}
    $api.get('job/list',data).then(res => {
      //请求成功
      that.setData({
        job:res.data.data
      })
    })
  },
  onShareAppMessage: function(){
    return {
        title: "天意美业直通车",
        path: "/pages/index/index?puid="+wx.getStorageSync('uid')
    };
  },
  order:function(e){
    var index = e.currentTarget.dataset.index
    this.setData({
      exp:this.data.workTime[index]
    })
  },
  sendNumSelect:function(e){
    var index = e.currentTarget.dataset.index
    var that = this
    if(Number(this.data.groupNum[index]) > Number(this.data.num)){
      wx.showToast({
        title:'可用群发数量为'+that.data.num+'不能大于可用群发数量',
        duration:2000,//显示时长
        mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
        icon:'none', //图标，支持"success"、"loading" 
      })
      return;
    }
    this.setData({
      sendNum:this.data.groupNum[index]
    })
  },
  changeCity:function(e){
    var cityArr = e.detail.value
    var city = cityArr[1].replace("市","");
    this.setData({
      region:cityArr,
      city:city
    })
  },
  send:function(){
    if(!this.data.message){
      wx.showToast({
        title:'请输入群发消息',
        duration:2000,//显示时长
        mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
        icon:'none', //图标，支持"success"、"loading" 
      })
      return;
    }
    if(!this.data.sendNum){
      wx.showToast({
        title:'请选择群发数量',
        duration:2000,//显示时长
        mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
        icon:'none', //图标，支持"success"、"loading" 
      })
      return;
    }
    var data ={
      job:this.data.job[this.data.index]['name'],
      city:this.data.city,
      exp:this.data.exp,
      message:this.data.message,
      uid:wx.getStorageSync('uid'),
      sendNum:this.data.sendNum,
      groupId:this.data.groupId
    }
    $api.post('group/send',data).then(res => {
      if(res.data.code == 1){
        wx.showToast({
          title:'发送成功',
          duration:3000,
          mask:true, 
          icon:'success',
          success:function(){
              setTimeout(function(){
                wx.navigateBack()
              },3000) //延迟时间 这里是
          }
        })
      }else{
        wx.showToast({
          title:res.data.msg,
          duration:2000,//显示时长
          mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon:'none', //图标，支持"success"、"loading" 
        })
        return;
      }
    })
  },
  bindJob:function(e){
    this.setData({
      index:e.detail.value
    })
  },
  bindSend:function(e){
    this.setData({
      message:e.detail.value
    })
  }
});